import { doAppReviewUsingPost } from '@/services/backend/appController';
import { ProTable } from '@ant-design/pro-components';
import '@umijs/max';
import { message, Modal } from 'antd';
import React from 'react';

interface Props {
    oldData?: API.App;
    visible: boolean;
    onSubmit: (values: API.ReviewRequest) => void;
    onCancel: () => void;
}

const enum ReviewType {
    APPROVE = 1,
    REJECT = 2,
}

/**
 * 创建弹窗
 * @param props
 * @constructor
 */
const CreateModal: React.FC<Props> = (props) => {
    const { oldData, visible, onSubmit, onCancel } = props;
    const columns = [
        {
            title: '审核信息',
            dataIndex: 'reviewMessage',
            valueType: 'textarea',
        },
        {
            title: '审核结果',
            dataIndex: 'reviewStatus',
            valueEnum: {
                [ReviewType.APPROVE]: {
                    text: '审核通过',
                },
                [ReviewType.REJECT]: {
                    text: '审核拒绝',
                },
            },
        },
    ]

    const handleReview = async (fields: API.ReviewRequest) => {
        const hide = message.loading('正在审核');
        try {
            await doAppReviewUsingPost({
                id: oldData?.id,
                reviewStatus: fields.reviewStatus,
                reviewMessage: fields.reviewMessage,
            });
            hide();
            message.success('审核成功');
            return true;
        } catch (error: any) {
            hide();
            message.error('审核失败，' + error.message);
            return false;
        }
    };

    return (
        <Modal
            destroyOnClose
            title={'创建'}
            open={visible}
            footer={null}
            onCancel={() => {
                onCancel?.();
            }}
        >
            <ProTable
                type="form"
                columns={columns}
                onSubmit={async (values: API.ReviewRequest) => {
                    const success = await handleReview(values);
                    if (success) {
                        onSubmit?.(values);
                    }
                }}
            />
        </Modal>
    );
};
export default CreateModal;
